	package com.erpanderp.wam.ac.model.service.travel;
	
	import java.util.List;
	
	import org.hibernate.HibernateException;
	import org.hibernate.Query;
	import org.hibernate.Session;
	
	import com.erpanderp.wam.util.HibernateUtil;
	
	public class MiscReimbursementDAO {
		
	     public String getLastGeneratedMiscExpenseFormCode(){
	    	
	 		List<String> codeList = null;
	
	 		try {
	 			Session session = HibernateUtil.getSessionFactory()
	 					.getCurrentSession();
	 			session.getTransaction().begin();
	 			String queryString = "select m.miscExpenseCode from MiscExpense m";
	
	 			Query query = session.createQuery(queryString);
	 			query.setCacheable(false);
	 			codeList = (List<String>) query.list();
	 			session.getTransaction().commit();
	 		} catch (HibernateException e) {
	 			e.printStackTrace();
	 		}
	
	 		String lastCode = null;
	 		for (String code : codeList) {
	 			if (lastCode == null) {
	 				lastCode = code;
	 				continue;
	
	 			} else if (Long
	 					.parseLong(code.substring(code.lastIndexOf("-") + 1)) > Long
	 					.parseLong(lastCode.substring(lastCode.lastIndexOf("-") + 1))) {
	 				lastCode = code;
	 				continue;
	 			}
	 			System.out.println(lastCode + "============ " + code);
	 		}
	 		return lastCode;
	     }
	}
